/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.dc.task;

import com.cxhd.dc.DataConfig;
import com.jfinal.kit.PropKit;
import java.io.File;
import java.util.Timer;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

/**
 *
 * @author Administrator
 */
public class StartTask {

    private Timer sfz;
    private Timer uf;
    private Timer df;
    private Timer szq;
    private Timer sp;
    private Timer pd;
    private Timer sfsp;

    private static StartTask startTask;

    private boolean startFlag = false;

    private ExecutorService service;//队列线程

    private BlockingQueue unzipQueue = new LinkedBlockingQueue<File>(); //解压队列

    private BlockingQueue sendPacsQueue = new LinkedBlockingQueue<String>(); //解压队列

    public static StartTask getIntance() {
        if (startTask == null) {
            startTask = new StartTask();
        }
        return startTask;
    }

    public void start() {
        
        if (!startFlag) {
            System.out.println("启动任务");
            sfz = new Timer();
            uf = new Timer();
            df = new Timer();
            szq = new Timer();
            sp = new Timer();
            pd = new Timer();
            sfsp = new Timer();
            sfz.schedule(new ScanFileToZipTask(), 1000, 1000 * 20);//压缩文件任务
            uf.schedule(new UploadFileTask(), 1000, 1000 * 20);//上传文件任务
            df.schedule(new DownloadFileTask(unzipQueue), 1000, 1000 * 30);//下载文件任务

           // szq.schedule(new ScanZipToQueueTask(unzipQueue), 1000, 1000 * 30);//扫描ZIP放进解压列队
            sfsp.schedule(new ScanFolderToSendPacsQueueTask(sendPacsQueue), 1000, 1000 * 20);//扫描发送到pacs
            if (PropKit.get("serverType") != null && PropKit.get("serverType").equalsIgnoreCase("center")) {
                pd.schedule(new ParsingDicom(), 1000, 1000 * 20);//解析diocm 发送到存储服务器
            } else {
               // sp.schedule(new SendPacsTask_back(), 1000, 1000 * 20);//发送去PACS
            }

            //执行多个解压线程
            service = Executors.newCachedThreadPool();
            int unzipThreadCount = DataConfig.getInstance().getUnzipThread();
            unzipThreadCount = unzipThreadCount == 0 ? 1 : unzipThreadCount;
//            for (int i = 0; i < unzipThreadCount; i++) {
//                service.execute(new UnzipTaskQueue(unzipQueue));
//            }
            
            int sendPacsThreadCount = DataConfig.getInstance().getSendPacsThread();
            sendPacsThreadCount = sendPacsThreadCount == 0 ? 1 : sendPacsThreadCount;
            for (int i = 0; i < sendPacsThreadCount; i++) {
                service.execute(new SendPacsQueue(sendPacsQueue));
            }
            startFlag = true;
        }

    }

    public void stop() {
        if (startFlag) {
            System.out.println("关闭任务");
            sfz.cancel();
            uf.cancel();
            df.cancel();
            szq.cancel();
            sp.cancel();
            pd.cancel();

            service.shutdownNow();
            startFlag = false;
        }
    }

}
